<?php
function html_select($opt,$value = '0',$attr='',$title = ''){
	?>
	<select <?php echo $attr;?> >
	<?php 
	if(!empty($title)){
		?>
		<option value=""><?php echo $title;?></option>
		<?php 
	}

	foreach ($opt as $k=>$v){
		?>
		<option <?php echo $k == $value ? ' selected="selected" ' : ''?> value="<?php echo $k;?>"><?php echo $v;?></option>
		<?php 
	}		
	?>
	</select>
	<?php 
}

function html_radio($opt,$value = '0',$attr=''){
	?>
	<?php 
	foreach ($opt as $k=>$v){
		?>
		<label>
			<input type="radio" <?php echo $k == $value ? ' checked="checked" ' : ''?> 
				value="<?php echo $k;?>"
				<?php echo $attr;?>
			/>&nbsp;<?php echo $v;?>&nbsp;&nbsp;
		</label>
		<?php 
	}		
	?>
	<?php 
}

function html_checkbox($opt,$value = array(),$attr=''){
	?>
	<?php 
	foreach ($opt as $k=>$v){
		?>
		<label>
			<input type="checkbox" <?php echo in_array($k,(array)$value) ? ' checked="checked" ' : ''?> 
				value="<?php echo $k;?>"
				<?php echo $attr;?>
			/>&nbsp;<?php echo $v;?>&nbsp;&nbsp;
		</label>
		<?php 
	}		
	?>
	<?php 
}

function html_img($src,$attr = ''){
	$base_url = config_item('base_url');
	$img_url = $base_url.'admin/statics/img/';
	$src = $img_url.$src;
	?>
	<img <?php echo $attr;?> src="<?php echo $src;?>"/>
	<?php 
}

function html_css($src){
	$base_url = config_item('base_url');
	$css_url = $base_url.'admin/statics/css/';
	$src = $css_url.$src;
	?>
	<link href="<?php echo $src;?>" type="text/css" rel="stylesheet">
	<?php 
	
}


function html_js($src){
	$base_url = config_item('base_url');
	$js_url = $base_url.'admin/statics/js/';
	$src = $js_url.$src;
	?>
	<script src="<?php echo $src;?>"></script>
	<?php 
	
}

function html_jscss($src){
	$base_url = config_item('base_url');
	$css_url = $base_url.'admin/statics/js/';
	$src = $css_url.$src;
	?>
	<link href="<?php echo $src;?>" type="text/css" rel="stylesheet">
	<?php 
}

function html_uppic($name,$value = ''){
	require_once(FCPATH.'/admin/views/inc_upload_script.php');
	$base_url = config_item('base_url');
	$static_url = $base_url.'admin/statics/';

	$path = '';	
	if(!empty($value)){
		$CI =&get_instance();

		$CI->db->from('images');
		$CI->db->where('id',$value);

		$query = $CI->db->get();
		$list = $query->result_array();
		$base_url = config_item('base_url');
		if(!empty($list[0])) $path = $base_url.$list[0]['l'];
	}

	?>
	<input type="file" id="file-<?php echo $name;?>"/>
	<input type="hidden" id="hidden-<?php echo $name;?>" name="<?php echo $name;?>" value="<?php echo $value;?>"/>
	<div id="some_file_queue-<?php echo $name;?>" class="upload_queue"></div>
	<div id="lc-<?php echo $name;?>" style="
		margin-left:10px;
		border:1px solid #eee;width:102px;height:102px;
		overflow:hidden;box-shadow: 0 1px 3px rgba(0,0,0,0.25);">
		<?php
		if(!empty($value)){
			?>
			<img width="100px" height="100px" src="<?php echo $path;?>"/>
			<?php
		}
		?>
	</div>

	<style type="text/css">
		.upload_queue {
		    background-color: #FFF;
		    height: 60px;
		    margin-bottom: 5px;
		    overflow: auto;
		    width: 300px;
		}
	</style> 

	<script>$(function(){
	    $('#file-<?php echo $name;?>').uploadify({
	        swf      : '<?php echo $static_url;?>js/uploadify/uploadify.swf',
	        uploader : '<?php echo site_url("c=data&m=upload");?>',
	        multi: false,
	        transparent: false,
	        queueID  : 'some_file_queue-<?php echo $name;?>',
	        onUploadSuccess : function(file, data, response){
	        	data = eval('('+data+')');
	        	var limg = '<img height="100" width="100" src="'+data.spath+'"/>';
	        	$('#lc-<?php echo $name;?>').empty().append(limg);
	        	$('#hidden-<?php echo $name;?>').val(data.id);
	        },
			buttonText:'本地上传'
	    }); 
	});</script>
	<?php		
}


function html_uppics($name,$value=''){
	require_once(FCPATH.'/admin/views/inc_upload_script.php');
	$base_url = config_item('base_url');
	$static_url = $base_url.'admin/statics/';

	$path = '';	
	if(!empty($value)){
		$CI =&get_instance();

		$CI->db->from('images');
		$CI->db->where_in('id',explode(',', trim($value,',')));

		$query = $CI->db->get();
		$list = $query->result_array();

	}


	?>

	<input type="file" id="file-<?php echo $name;?>"/>
	<input type="hidden" id="hidden-<?php echo $name;?>" name="<?php echo $name;?>" value="<?php echo $value;?>"/>
	<div id="lc-<?php echo $name;?>">
		<?php if(!empty($list)):?>
		<?php foreach ($list as $k => $v):?>
		<img height="100" width="100" src="<?php echo $base_url.$v['l']?>"/>&nbsp;
		<?php endforeach;?>
		<?php endif;?>
	</div>

	<div id="some_file_queue-<?php echo $name;?>"></div>

	<script>$(function(){
	    $('#file-<?php echo $name;?>').uploadify({
	        swf      : '<?php echo $static_url;?>js/uploadify/uploadify.swf',
	        uploader : '<?php echo site_url("c=data&m=upload");?>',
	        multi: true,
	        transparent: false,
	        queueID  : 'some_file_queue-<?php echo $name;?>',
	        onUploadSuccess : function(file, data, response){

	        	data = eval('('+data+')');
	        	var limg = '<img height="100" width="100" src="'+data.spath+'"/>&nbsp;';
	        	$('#lc-<?php echo $name;?>').append(limg);
	        	var o_val = $('#hidden-<?php echo $name;?>').val();
	        	$('#hidden-<?php echo $name;?>').val(o_val+','+data.id);
	        },
			buttonText:'本地上传'
	    }); 
	});</script>

	<?php		
}

function html_html($name,$value = ''){
	require_once(FCPATH.'/admin/views/inc_script.php');

	?>
	<script type="text/javascript">
	$(function(){

		$('#xheditor-<?php echo $name;?>').xheditor({
			upLinkUrl:'<?php echo site_url("c=upload&m=xhimg&immediate=1");?>',
			upLinkExt:"zip,rar,txt",
			upImgUrl:'<?php echo site_url("c=upload&m=xhimg&immediate=1");?>',
			upImgExt:"jpg,jpeg,gif,png",
			upFlashUrl:"upload.php?immediate=1",
			upFlashExt:"swf",
			upMediaUrl:"upload.php?immediate=1",
			upMediaExt:"avi"
		});

	});
	</script>
	<textarea id="xheditor-<?php echo $name;?>" name="<?php echo $name;?>" style="width:600px;height:200px;"><?php echo $value;?></textarea>
	<?php
}




function html_attr_view($params){
	$type = $params['type'];
	$name = $params['name'];
	$define = $params['value'];
	$value = '';

	$opt = array();

	$arr_t = explode(';', $define);
	foreach ($arr_t as $k => $v) {
		if(empty($v)) continue;
		$arr_tt = explode(':', $v);
		if(empty($arr_tt[0])) continue;
		$opt[$arr_tt[0]] = $arr_tt[1];
	}

	$validate = '';
	if($params['allow_null']){
		$validate = ' validate[required] ';
	}

	if($type == 'text'){
		?>
		<input style="width:300px;" class="<?php echo $validate;?>" type="text"  name="<?php echo $name;?>"/>
		<?php
	}elseif($type == 'int'){
		?>
		<input style="width:120px;" type="text" class="<?php echo $validate;?>" name="<?php echo $name;?>"/>
		<?php
	}elseif($type == 'textarea'){
		?>
		<textarea class="ta-2 <?php echo $validate;?>"  name="<?php echo $name;?>" ><?php echo $value;?></textarea>
		<?php
	}elseif($type == 'select'){
		html_select($opt,$value,'class="'.$validate.'" name="'.$name.'"','---请选择--');
	}elseif($type == 'radio'){		
		html_radio($opt,$value,'class="'.$validate.'" name="'.$name.'"');
	}elseif($type == 'checkbox'){
		html_checkbox($opt,$value,'name="'.$name.'"');
	}elseif($type == 'image'){
		require_once(FCPATH.'/admin/views/inc_upload_script.php');
		$base_url = config_item('base_url');
		$static_url = $base_url.'admin/statics/';
		?>
		<input type="file" id="file-<?php echo $name;?>"/>
		<input type="hidden" id="hidden-<?php echo $name;?>" name="<?php echo $name;?>"/>
		<div id="some_file_queue" class="upload_queue"></div>
		<div id="lc-<?php echo $name;?>" style="
			margin-left:10px;
			border:1px solid #eee;width:102px;height:102px;
			overflow:hidden;box-shadow: 0 1px 3px rgba(0,0,0,0.25);"></div>

		<style type="text/css">
			.upload_queue {
			    background-color: #FFF;
			    height: 60px;
			    margin-bottom: 5px;
			    overflow: auto;
			    width: 300px;
			}
		</style> 

		<script>$(function(){
		    $('#file-<?php echo $name;?>').uploadify({
		        swf      : '<?php echo $static_url;?>js/uploadify/uploadify.swf',
		        uploader : '<?php echo $base_url;?>upload.php',
		        multi: false,
		        transparent: false,
		        queueID  : 'some_file_queue',
		        onUploadSuccess : function(file, data, response){
		        	data = eval('('+data+')');
		        	var limg = '<img height="100" width="100" src="'+data.spath+'"/>';
		        	$('#lc-<?php echo $name;?>').empty().append(limg);
		        	$('#hidden-<?php echo $name;?>').val(data.id);
		        },
				buttonText:'本地上传'
		    }); 
		});</script>
		<?php		
		
	}elseif($type == 'images'){

		require_once(FCPATH.'/admin/views/inc_upload_script.php');
		$base_url = config_item('base_url');
		$static_url = $base_url.'admin/statics/';

		?>

		<input type="file" id="file-<?php echo $name;?>"/>
		<input type="hidden" id="hidden-<?php echo $name;?>" name="<?php echo $name;?>"/>
		<div id="lc-<?php echo $name;?>"></div>

		<div id="some_file_queue-<?php echo $name;?>"></div>

		<script>$(function(){
		    $('#file-<?php echo $name;?>').uploadify({
		        swf      : '<?php echo $static_url;?>js/uploadify/uploadify.swf',
		        uploader : '<?php echo $base_url;?>upload.php',
		        multi: true,
		        transparent: false,
		        queueID  : 'some_file_queue-<?php echo $name;?>',
		        onUploadSuccess : function(file, data, response){
		        	data = eval('('+data+')');
		        	var limg = '<img height="100" width="100" src="'+data.spath+'"/>&nbsp;';
		        	$('#lc-<?php echo $name;?>').append(limg);
		        	var o_val = $('#hidden-<?php echo $name;?>').val();
		        	$('#hidden-<?php echo $name;?>').val(o_val+data.id+',');
		        },
				buttonText:'本地上传'
		    }); 
		});</script>

		<?php		

	}elseif($type == 'html'){	
		require_once(FCPATH.'/admin/views/inc_script.php');
		?>
		<textarea name="<?php echo $name;?>" style="width:600px;height:200px;" class="xhEditor <?php echo $validate;?>"  ><?php echo $value;?></textarea>
		<?php
	}
}


